package com.lizardmind.haofudoctor.ui.studio.sickEducation

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.linxiao.framework.adapter.BaseAdapter
import com.linxiao.framework.fragment.BaseFragment
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.util.ke
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.FragmentSystemEduBinding
import com.lizardmind.haofudoctor.databinding.ItemSystemEdu1Binding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.other.SystemEduBean
import com.lizardmind.haofudoctor.ui.other.PatientsEduWebActivity
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.fragment_system_edu.*
import org.jetbrains.anko.bundleOf
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.support.v4.act
import org.jetbrains.anko.support.v4.startActivity

/**
 * Created by Administrator on 2017/12/11.
 * 养生资讯Fragment
 */
class SystemEduFragment : BaseFragment(){
    companion object {
        fun getInstance(type:Int):SystemEduFragment{
            val fra = SystemEduFragment()
            fra.arguments = bundleOf("type" to type)
            return fra
        }
    }
    private val count="10"
    private var p=0
    private val type by lazy { arguments["type"] as Int }

    private val list = arrayListOf<SystemEduBean>()
    val adapter by lazy { BaseAdapter<SystemEduBean, ItemSystemEdu1Binding>(R.layout.item_system_edu1, list) }

    val binding by lazy { getDataBinding<FragmentSystemEduBinding>(R.layout.fragment_system_edu) }
    override fun onCreatRootView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        rvSystemEdu.adapter=adapter
        rvSystemEdu.layoutManager= LinearLayoutManager(context)

        refreshLayout.startRefresh()
        refreshLayout.setOnRefreshListener(object : RefreshListenerAdapter(){
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                list.clear()
                p=0
                getData{ refreshLayout?.finishRefreshing() }

            }

            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                super.onLoadMore(refreshLayout)
                p++
                getData{refreshLayout?.finishLoadmore()}
            }
        })

        adapter.onBind { itemBingding, position, data ->
            itemBingding.root.onClick {
                startActivity<PatientsEduWebActivity>("url" to data.url,"uid" to (activity.intent.getStringExtra("uid")?:""))
            }
        }
    }

    //false加载更多  true刷新
    private fun getData(onComplete: ()->Unit){
        observable {
            postApi<Request<ArrayList<SystemEduBean>>>(
                    hashMapOf("action" to "patient_ku",
                            "token" to DoctorInfo.token,
                            "type" to type.toString(),
                            "p" to p.toString(),
                            "count" to count),
                    Consumer {
                        it.dispose {
                            list.addAll(it)
                            adapter.notifyDataSetChanged()
                        }
                    }, Action { onComplete() })
        }
    }
}